package net.xju3_4.controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.common.utils.Result;
import net.xju3_4.entity.MerchantApplicationEntity;
import net.xju3_4.service.MerchantApplicationService;
import net.xju3_4.service.impl.MerchantApplicationServiceImpl;
import java.io.UnsupportedEncodingException;
@RequestMapping("/api/admin/merchantapplication")
public class AdminMerchantApplicationController {
    private MerchantApplicationService merchantApplicationService = new MerchantApplicationServiceImpl();
    @RequestMapping("/pagelist.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String pageNoStr = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageNo = pageNoStr == null || pageSizeStr.isEmpty() ? 1 : Integer.parseInt(pageNoStr);
        Integer pageSize =pageNoStr==null || pageSizeStr.isEmpty() ? 10 : Integer.parseInt(pageSizeStr);
        String username=req.getParameter("username");
        String merchantName=req.getParameter("merchantName");
        String address=req.getParameter("address");
        MerchantApplicationEntity application = new MerchantApplicationEntity();
        if(username != null) application.setUsername(username);
        if(merchantName != null) application.setMerchantName(merchantName);
        if(address != null) application.setAddress( address);
        PageInfo<MerchantApplicationEntity> pageInfo = merchantApplicationService.pagelist(pageNo, pageSize, application);
        if(pageInfo != null)
        JSONUtil.printByJSON(resp,Result.success(pageInfo,"修改成功"));
        else
            JSONUtil.printByJSON(resp,Result.fail("修改失败"));
    }

    @RequestMapping("/accept.do")
    public void accept(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Integer id = Integer.parseInt(req.getParameter("id"));
        if(merchantApplicationService.accept(id)>0)
            JSONUtil.printByJSON(resp,Result.success("已通过商家申请"));
        else
            JSONUtil.printByJSON(resp,Result.fail("操作失败"));
    }

    @RequestMapping("/reject.do")
    public void reject(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        Integer id = Integer.parseInt(req.getParameter("id"));
        if(merchantApplicationService.reject(id)>0)
            JSONUtil.printByJSON(resp,Result.success("已拒绝商家申请"));
        else
            JSONUtil.printByJSON(resp,Result.fail("操作失败"));
    }
    @RequestMapping("/deleted.do")
    public void deleted(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        Integer id = Integer.parseInt(req.getParameter("id"));
        if(merchantApplicationService.deleted(id)>0)
            JSONUtil.printByJSON(resp,Result.success("已删除商家申请"));
        else
            JSONUtil.printByJSON(resp,Result.fail("操作失败"));
    }
}
